﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Telerik.Web.UI;
using KPIS.GERP.ESS.APP.App_UI.Commons;
using KPIS.GERP.ESS.BLL;
using KPIS.GERP.ESS.MODEL;

namespace KPIS.GERP.ESS.APP.App_UI.UserControls
{
    public partial class DropDownList_Contact : System.Web.UI.UserControl
    {
        public RadComboBox Child;
        int nOffset = 10;
        Main main = new Main();
        FIM_MAS_Contacts.FIM_MAS_Contacts con = new FIM_MAS_Contacts.FIM_MAS_Contacts();

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        public RadComboBox GetComboBox()
        {
            RadComboBox ddl = ddlContactSeq;
            return ddl;
        }

        public void LoadData(RadComboBoxItemsRequestedEventArgs e)
        {
            LoadData(e, null);
        }

        public void LoadData(RadComboBoxItemsRequestedEventArgs e, string strContactSeq)
        {
            string strName = lblName.Text;
            string strValue = lblValue.Text;
            string strFilter = e.Text;
            IList<FIM_MAS_Contacts.M_FIM_MAS_Contacts> infolist = null;

            switch (strName)
            {
                case "Offer" :
                    infolist = con.GetListOffer(strValue, strContactSeq, strFilter);
                    break;
                default :
                    infolist = con.GetList(strFilter);
                    break;
            }
            
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + nOffset, infolist.Count);
            e.EndOfItems = endOffset == infolist.Count;

            //ddlContactSeq.Items.Clear();

            for (int i = itemOffset; i < endOffset; i++)
            {
                ddlContactSeq.Items.Add(new RadComboBoxItem(infolist[i].contact_name, infolist[i].contact_seq.ToString()));
            }

            e.Message = main.GetStatusMessage(endOffset, infolist.Count);
        }

        public void LoadControl(Nullable<int> nWidth, Nullable<int> nSetOffset
            , string strName, string strValue)
        {
            if (nWidth != null)
                ddlContactSeq.Width = Unit.Pixel(nWidth.Value);

            if (nSetOffset != null)
                nOffset = nSetOffset.Value;

            lblName.Text = strName;
            lblValue.Text = strValue;
        }

        public void LoadControl(Nullable<int> nWidth, Nullable<int> nSetOffset)
        {
            LoadControl(nWidth, nSetOffset, "", "");
        }

        public void SetSelectedValue(string strValue)
        {
            if (strValue == null)
            {
                strValue = "";
            }

            if (strValue.Length > 0)
            {
                lblContactSeq.Text = strValue;

                if (ddlContactSeq.FindItemByValue(strValue) == null)
                {
                    FIM_MAS_Contacts.M_FIM_MAS_Contacts info = con.GetInfo(strValue);
                    ddlContactSeq.Text = info.contact_name;
                }
                else
                {
                    ddlContactSeq.SelectedValue = strValue;
                }
            }
            else
            {
                ddlContactSeq.SelectedIndex = -1;
                ddlContactSeq.Text = "";
                ddlContactSeq.Text = "";
            }
        }

        public string GetSelectedValue()
        {
            string strValue = lblContactSeq.Text;

            return strValue;
        }

        protected void ddlContactSeq_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            //lblContactSeq.Text = ddlContactSeq.SelectedValue;

            //if (Child != null)
            //{
            //    Child.Text = "";

            //    if (ddlContactSeq.SelectedValue.Length > 0)
            //    {
            //        switch (Child.ID)
            //        {
            //            case "ddlContactPersonSeq":
            //                App_UI.UserControls.DropDownList_ContactPerson uc = (App_UI.UserControls.DropDownList_ContactPerson)Child.Parent;
            //                uc.LoadControl(null, null, ddlContactSeq.SelectedValue);
            //                break;
            //        }
            //    }
            //    else
            //    {
            //        Child.DataSource = null;
            //        Child.DataBind();
            //    }
            //}
        }

        protected void ddlContactSeq_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            LoadData(e);
        }
    }
}